<!-- HTML header for doxygen 1.8.6-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<title>OpenCV: Classify</title>
<link href="../../opencv.ico" rel="shortcut icon" type="image/x-icon" />
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="../../tutorial-utils.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
//<![CDATA[
MathJax.Hub.Config(
{
  TeX: {
      Macros: {
          matTT: [ "\\[ \\left|\\begin{array}{ccc} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{array}\\right| \\]", 9],
          fork: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ \\end{array} \\right.", 4],
          forkthree: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ \\end{array} \\right.", 6],
          forkfour: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ #7 & \\mbox{#8}\\\\ \\end{array} \\right.", 8],
          vecthree: ["\\begin{bmatrix} #1\\\\ #2\\\\ #3 \\end{bmatrix}", 3],
          vecthreethree: ["\\begin{bmatrix} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{bmatrix}", 9],
          cameramatrix: ["#1 = \\begin{bmatrix} f_x & 0 & c_x\\\\ 0 & f_y & c_y\\\\ 0 & 0 & 1 \\end{bmatrix}", 1],
          distcoeffs: ["(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \\tau_x, \\tau_y]]]]) \\text{ of 4, 5, 8, 12 or 14 elements}"],
          distcoeffsfisheye: ["(k_1, k_2, k_3, k_4)"],
          hdotsfor: ["\\dots", 1],
          mathbbm: ["\\mathbb{#1}", 1],
          bordermatrix: ["\\matrix{#1}", 1]
      }
  }
}
);
//]]>
</script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
<link href="../../stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<!--#include virtual="/google-search.html"-->
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="../../opencv-logo-small.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">OpenCV
   &#160;<span id="projectnumber">4.5.2</span>
   </div>
   <div id="projectbrief">Open Source Computer Vision</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('../../',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="../../d3/d81/tutorial_contrib_root.html">Tutorials for contrib modules</a></li><li class="navelem"><a class="el" href="../../d2/d42/tutorial_table_of_content_cnn_3dobj.html">CNN for 3D Object Classification and Pose Estimation</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">Classify </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><h2>Goal </h2>
<p>In this tutorial you will learn how to</p>
<ul>
<li>How to extract feature from an image</li>
<li>How to extract features from images under a given root path</li>
<li>How to make a prediction using reference images and target image</li>
</ul>
<h2>Code </h2>
<div class="fragment"><div class="line"><span class="comment">/*</span></div><div class="line"><span class="comment"> * Software License Agreement (BSD License)</span></div><div class="line"><span class="comment"> *</span></div><div class="line"><span class="comment"> *  Copyright (c) 2009, Willow Garage, Inc.</span></div><div class="line"><span class="comment"> *  All rights reserved.</span></div><div class="line"><span class="comment"> *</span></div><div class="line"><span class="comment"> *  Redistribution and use in source and binary forms, with or without</span></div><div class="line"><span class="comment"> *  modification, are permitted provided that the following conditions</span></div><div class="line"><span class="comment"> *  are met:</span></div><div class="line"><span class="comment"> *</span></div><div class="line"><span class="comment"> *   * Redistributions of source code must retain the above copyright</span></div><div class="line"><span class="comment"> *     notice, this list of conditions and the following disclaimer.</span></div><div class="line"><span class="comment"> *   * Redistributions in binary form must reproduce the above</span></div><div class="line"><span class="comment"> *     copyright notice, this list of conditions and the following</span></div><div class="line"><span class="comment"> *     disclaimer in the documentation and/or other materials provided</span></div><div class="line"><span class="comment"> *     with the distribution.</span></div><div class="line"><span class="comment"> *   * Neither the name of Willow Garage, Inc. nor the names of its</span></div><div class="line"><span class="comment"> *     contributors may be used to endorse or promote products derived</span></div><div class="line"><span class="comment"> *     from this software without specific prior written permission.</span></div><div class="line"><span class="comment"> *</span></div><div class="line"><span class="comment"> *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS</span></div><div class="line"><span class="comment"> *  &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT</span></div><div class="line"><span class="comment"> *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS</span></div><div class="line"><span class="comment"> *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE</span></div><div class="line"><span class="comment"> *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,</span></div><div class="line"><span class="comment"> *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,</span></div><div class="line"><span class="comment"> *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;</span></div><div class="line"><span class="comment"> *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER</span></div><div class="line"><span class="comment"> *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT</span></div><div class="line"><span class="comment"> *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN</span></div><div class="line"><span class="comment"> *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE</span></div><div class="line"><span class="comment"> *  POSSIBILITY OF SUCH DAMAGE.</span></div><div class="line"><span class="comment"> *</span></div><div class="line"><span class="comment"> */</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../de/d45/cnn__3dobj_8hpp.html">opencv2/cnn_3dobj.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../d5/d0d/features2d_8hpp.html">opencv2/features2d.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;iomanip&gt;</span></div><div class="line"><span class="keyword">using namespace </span><a class="code" href="../../d2/d75/namespacecv.html">cv</a>;</div><div class="line"><span class="keyword">using namespace </span>std;</div><div class="line"><span class="keyword">using namespace </span><a class="code" href="../../d9/d57/namespacecv_1_1cnn__3dobj.html">cv::cnn_3dobj</a>;</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> listDir(<span class="keyword">const</span> <span class="keywordtype">char</span> *path, std::vector&lt;String&gt;&amp; files, <span class="keywordtype">bool</span> r)</div><div class="line">{</div><div class="line">    DIR *pDir;</div><div class="line">    <span class="keyword">struct </span>dirent *ent;</div><div class="line">    <span class="keywordtype">char</span> childpath[512];</div><div class="line">    pDir = opendir(path);</div><div class="line">    memset(childpath, 0, <span class="keyword">sizeof</span>(childpath));</div><div class="line">    <span class="keywordflow">while</span> ((ent = readdir(pDir)) != NULL)</div><div class="line">    {</div><div class="line">        <span class="keywordflow">if</span> (ent-&gt;d_type &amp; DT_DIR)</div><div class="line">        {</div><div class="line">            <span class="keywordflow">if</span> (strcmp(ent-&gt;d_name, <span class="stringliteral">&quot;.&quot;</span>) == 0 || strcmp(ent-&gt;d_name, <span class="stringliteral">&quot;..&quot;</span>) == 0 || strcmp(ent-&gt;d_name, <span class="stringliteral">&quot;.DS_Store&quot;</span>) == 0)</div><div class="line">            {</div><div class="line">                <span class="keywordflow">continue</span>;</div><div class="line">            }</div><div class="line">            <span class="keywordflow">if</span> (r)</div><div class="line">            {</div><div class="line">                sprintf(childpath, <span class="stringliteral">&quot;%s/%s&quot;</span>, path, ent-&gt;d_name);</div><div class="line">                listDir(childpath,files,<span class="keyword">false</span>);</div><div class="line">            }</div><div class="line">        }</div><div class="line">        <span class="keywordflow">else</span></div><div class="line">        {</div><div class="line">            <span class="keywordflow">if</span> (strcmp(ent-&gt;d_name, <span class="stringliteral">&quot;.DS_Store&quot;</span>) != 0)</div><div class="line">                files.push_back(ent-&gt;d_name);</div><div class="line">        }</div><div class="line">    }</div><div class="line">    <a class="code" href="../../d2/de8/group__core__array.html#ga45dd56da289494ce874be2324856898f">sort</a>(files.begin(),files.end());</div><div class="line">};</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">int</span> featureWrite(<span class="keyword">const</span> <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> &amp;features, <span class="keyword">const</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> &amp;fname)</div><div class="line">{</div><div class="line">    ofstream ouF;</div><div class="line">    ouF.open(fname.c_str(), std::ofstream::binary);</div><div class="line">    <span class="keywordflow">if</span> (!ouF)</div><div class="line">    {</div><div class="line">        cerr &lt;&lt; <span class="stringliteral">&quot;failed to open the file : &quot;</span> &lt;&lt; fname &lt;&lt; endl;</div><div class="line">        <span class="keywordflow">return</span> 0;</div><div class="line">    }</div><div class="line">    <span class="keywordflow">for</span> (<span class="keywordtype">int</span> r = 0; r &lt; features.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abed816466c45234254d25bc59c31245e">rows</a>; r++)</div><div class="line">    {</div><div class="line">        ouF.write(reinterpret_cast&lt;const char*&gt;(features.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#a13acd320291229615ef15f96ff1ff738">ptr</a>(r)), features.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa3e5a47585c9ef6a0842556739155e3e">cols</a>*features.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aba7a5ef00b22b70668fba575005dfe55">elemSize</a>());</div><div class="line">    }</div><div class="line">    ouF.close();</div><div class="line">    <span class="keywordflow">return</span> 1;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span>** argv)</div><div class="line">{</div><div class="line">    <span class="keyword">const</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> keys = <span class="stringliteral">&quot;{help | | This sample will extract features from reference images and target image for classification. You can add a mean_file if there little variance in data such as human faces, otherwise it is not so useful}&quot;</span></div><div class="line">    <span class="stringliteral">&quot;{src_dir | ../data/images_all/ | Source direction of the images ready for being used for extract feature as gallery.}&quot;</span></div><div class="line">    <span class="stringliteral">&quot;{caffemodel | ../../testdata/cv/3d_triplet_iter_30000.caffemodel | caffe model for feature exrtaction.}&quot;</span></div><div class="line">    <span class="stringliteral">&quot;{network_forIMG | ../../testdata/cv/3d_triplet_testIMG.prototxt | Network definition file used for extracting feature from a single image and making a classification}&quot;</span></div><div class="line">    <span class="stringliteral">&quot;{mean_file | no | The mean file generated by Caffe from all gallery images, this could be used for mean value substraction from all images. If you want to use the mean file, you can set this as ../data/images_mean/triplet_mean.binaryproto.}&quot;</span></div><div class="line">    <span class="stringliteral">&quot;{target_img | ../data/images_all/4_78.png | Path of image waiting to be classified.}&quot;</span></div><div class="line">    <span class="stringliteral">&quot;{feature_blob | feat | Name of layer which will represent as the feature, in this network, ip1 or feat is well.}&quot;</span></div><div class="line">    <span class="stringliteral">&quot;{num_candidate | 15 | Number of candidates in gallery as the prediction result.}&quot;</span></div><div class="line">    <span class="stringliteral">&quot;{device | CPU | Device type: CPU or GPU}&quot;</span></div><div class="line">    <span class="stringliteral">&quot;{dev_id | 0 | Device id}&quot;</span></div><div class="line">    <span class="stringliteral">&quot;{gallery_out | 0 | Option on output binary features on gallery images}&quot;</span>;</div><div class="line">    <span class="comment">/* get parameters from comand line */</span></div><div class="line">    <a class="code" href="../../d0/d2e/classcv_1_1CommandLineParser.html">cv::CommandLineParser</a> parser(argc, argv, keys);</div><div class="line">    parser.about(<span class="stringliteral">&quot;Feature extraction and classification&quot;</span>);</div><div class="line">    <span class="keywordflow">if</span> (parser.has(<span class="stringliteral">&quot;help&quot;</span>))</div><div class="line">    {</div><div class="line">        parser.printMessage();</div><div class="line">        <span class="keywordflow">return</span> 0;</div><div class="line">    }</div><div class="line">    <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> src_dir = parser.get&lt;<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>&gt;(<span class="stringliteral">&quot;src_dir&quot;</span>);</div><div class="line">    <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> caffemodel = parser.get&lt;<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>&gt;(<span class="stringliteral">&quot;caffemodel&quot;</span>);</div><div class="line">    <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> network_forIMG   = parser.get&lt;<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>&gt;(<span class="stringliteral">&quot;network_forIMG&quot;</span>);</div><div class="line">    <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> mean_file    = parser.get&lt;<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>&gt;(<span class="stringliteral">&quot;mean_file&quot;</span>);</div><div class="line">    <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> target_img   = parser.get&lt;<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>&gt;(<span class="stringliteral">&quot;target_img&quot;</span>);</div><div class="line">    <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> feature_blob = parser.get&lt;<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>&gt;(<span class="stringliteral">&quot;feature_blob&quot;</span>);</div><div class="line">    <span class="keywordtype">int</span> num_candidate = parser.get&lt;<span class="keywordtype">int</span>&gt;(<span class="stringliteral">&quot;num_candidate&quot;</span>);</div><div class="line">    <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> device = parser.get&lt;<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>&gt;(<span class="stringliteral">&quot;device&quot;</span>);</div><div class="line">    <span class="keywordtype">int</span> gallery_out = parser.get&lt;<span class="keywordtype">int</span>&gt;(<span class="stringliteral">&quot;gallery_out&quot;</span>);</div><div class="line">    <span class="comment">/* Initialize a net work with Device */</span></div><div class="line">    <a class="code" href="../../d7/d8d/classcv_1_1cnn__3dobj_1_1descriptorExtractor.html">cv::cnn_3dobj::descriptorExtractor</a> descriptor(device);</div><div class="line">    std::cout &lt;&lt; <span class="stringliteral">&quot;Using&quot;</span> &lt;&lt; descriptor.getDeviceType() &lt;&lt; std::endl;</div><div class="line">    <span class="comment">/* Load net with the caffe trained net work parameter and structure */</span></div><div class="line">    <span class="keywordflow">if</span> (strcmp(mean_file.c_str(), <span class="stringliteral">&quot;no&quot;</span>) == 0)</div><div class="line">        descriptor.loadNet(network_forIMG, caffemodel);</div><div class="line">    <span class="keywordflow">else</span></div><div class="line">        descriptor.loadNet(network_forIMG, caffemodel, mean_file);</div><div class="line">    std::vector&lt;String&gt; name_gallery;</div><div class="line">    <span class="comment">/* List the file names under a given path */</span></div><div class="line">    listDir(src_dir.c_str(), name_gallery, <span class="keyword">false</span>);</div><div class="line">    <span class="keywordflow">if</span> (gallery_out)</div><div class="line">    {</div><div class="line">        ofstream namelist_out(<span class="stringliteral">&quot;gallelist.txt&quot;</span>);</div><div class="line">        <span class="comment">/* Writing name of the reference images. */</span></div><div class="line">        <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i = 0; i &lt; name_gallery.size(); i++)</div><div class="line">            namelist_out &lt;&lt; name_gallery.at(i) &lt;&lt; endl;</div><div class="line">    }</div><div class="line">    <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i = 0; i &lt; name_gallery.size(); i++)</div><div class="line">    {</div><div class="line">        name_gallery[i] = src_dir + name_gallery[i];</div><div class="line">    }</div><div class="line">    std::vector&lt;cv::Mat&gt; img_gallery;</div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">cv::Mat</a> feature_reference;</div><div class="line">    <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i = 0; i &lt; name_gallery.size(); i++)</div><div class="line">    {</div><div class="line">        img_gallery.push_back(<a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">cv::imread</a>(name_gallery[i]));</div><div class="line">    }</div><div class="line">    <span class="comment">/* Extract feature from a set of images */</span></div><div class="line">    descriptor.extract(img_gallery, feature_reference, feature_blob);</div><div class="line">    <span class="keywordflow">if</span> (gallery_out)</div><div class="line">    {</div><div class="line">        std::cout &lt;&lt; std::endl &lt;&lt; <span class="stringliteral">&quot;---------- Features of gallery images ----------&quot;</span> &lt;&lt; std::endl;</div><div class="line">        <span class="comment">/* Print features of the reference images. */</span></div><div class="line">        <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; feature_reference.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abed816466c45234254d25bc59c31245e">rows</a>; i++)</div><div class="line">            std::cout &lt;&lt; feature_reference.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#a4b22e1c23af7a7f2eef8fa478cfa7434">row</a>(i) &lt;&lt; endl;</div><div class="line">        std::cout &lt;&lt; std::endl &lt;&lt; <span class="stringliteral">&quot;---------- Saving features of gallery images into feature.bin ----------&quot;</span> &lt;&lt; std::endl;</div><div class="line">        featureWrite(feature_reference, <span class="stringliteral">&quot;feature.bin&quot;</span>);</div><div class="line">    }</div><div class="line">    <span class="keywordflow">else</span></div><div class="line">    {</div><div class="line">        std::cout &lt;&lt; std::endl &lt;&lt; <span class="stringliteral">&quot;---------- Prediction for &quot;</span> &lt;&lt; target_img &lt;&lt; <span class="stringliteral">&quot; ----------&quot;</span> &lt;&lt; std::endl;</div><div class="line">        <a class="code" href="../../d3/d63/classcv_1_1Mat.html">cv::Mat</a> img = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">cv::imread</a>(target_img);</div><div class="line">        std::cout &lt;&lt; std::endl &lt;&lt; <span class="stringliteral">&quot;---------- Features of gallery images ----------&quot;</span> &lt;&lt; std::endl;</div><div class="line">        std::vector&lt;std::pair&lt;String, float&gt; &gt; prediction;</div><div class="line">        <span class="comment">/* Print features of the reference images. */</span></div><div class="line">        <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; feature_reference.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abed816466c45234254d25bc59c31245e">rows</a>; i++)</div><div class="line">            std::cout &lt;&lt; feature_reference.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#a4b22e1c23af7a7f2eef8fa478cfa7434">row</a>(i) &lt;&lt; endl;</div><div class="line">        <a class="code" href="../../d3/d63/classcv_1_1Mat.html">cv::Mat</a> feature_test;</div><div class="line">        descriptor.extract(img, feature_test, feature_blob);</div><div class="line">        <span class="comment">/* Initialize a matcher which using L2 distance. */</span></div><div class="line">        <a class="code" href="../../d3/da1/classcv_1_1BFMatcher.html">cv::BFMatcher</a> matcher(<a class="code" href="../../d2/de8/group__core__array.html#ggad12cefbcb5291cf958a85b4b67b6149fa7bacbe84d400336a8f26297d8e80e3a2">NORM_L2</a>);</div><div class="line">        std::vector&lt;std::vector&lt;cv::DMatch&gt; &gt; matches;</div><div class="line">        <span class="comment">/* Have a KNN match on the target and reference images. */</span></div><div class="line">        matcher.knnMatch(feature_test, feature_reference, matches, num_candidate);</div><div class="line">        <span class="comment">/* Print feature of the target image waiting to be classified. */</span></div><div class="line">        std::cout &lt;&lt; std::endl &lt;&lt; <span class="stringliteral">&quot;---------- Features of target image: &quot;</span> &lt;&lt; target_img &lt;&lt; <span class="stringliteral">&quot;----------&quot;</span> &lt;&lt; endl &lt;&lt; feature_test &lt;&lt; std::endl;</div><div class="line">        <span class="comment">/* Print the top N prediction. */</span></div><div class="line">        std::cout &lt;&lt; std::endl &lt;&lt; <span class="stringliteral">&quot;---------- Prediction result(Distance - File Name in Gallery) ----------&quot;</span> &lt;&lt; std::endl;</div><div class="line">        <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> i = 0; i &lt; matches[0].size(); ++i)</div><div class="line">        {</div><div class="line">            std::cout &lt;&lt; i &lt;&lt; <span class="stringliteral">&quot; - &quot;</span> &lt;&lt; std::fixed &lt;&lt; std::setprecision(2) &lt;&lt; name_gallery[matches[0][i].trainIdx] &lt;&lt; <span class="stringliteral">&quot; - \&quot;&quot;</span>  &lt;&lt; matches[0][i].distance &lt;&lt; <span class="stringliteral">&quot;\&quot;&quot;</span> &lt;&lt; std::endl;</div><div class="line">        }</div><div class="line">    }</div><div class="line">    <span class="keywordflow">return</span> 0;</div><div class="line">}</div></div><!-- fragment --><h2>Explanation </h2>
<p>Here is the general structure of the program:</p>
<ul>
<li>Initialize a net work with Device. <div class="fragment"><div class="line"><a class="code" href="../../d7/d8d/classcv_1_1cnn__3dobj_1_1descriptorExtractor.html">cv::cnn_3dobj::descriptorExtractor</a> descriptor(device);</div></div><!-- fragment --></li>
<li>Load net with the caffe trained net work parameter and structure. <div class="fragment"><div class="line"><span class="keywordflow">if</span> (strcmp(mean_file.c_str(), <span class="stringliteral">&quot;no&quot;</span>) == 0)</div><div class="line">    descriptor.loadNet(network_forIMG, caffemodel);</div><div class="line"><span class="keywordflow">else</span></div><div class="line">    descriptor.loadNet(network_forIMG, caffemodel, mean_file);</div></div><!-- fragment --></li>
<li>List the file names under a given path. <div class="fragment"><div class="line">listDir(src_dir.c_str(), name_gallery, <span class="keyword">false</span>);</div><div class="line"><span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i = 0; i &lt; name_gallery.size(); i++)</div><div class="line">{</div><div class="line">    name_gallery[i] = src_dir + name_gallery[i];</div><div class="line">}</div></div><!-- fragment --></li>
<li>Extract feature from a set of images. <div class="fragment"><div class="line">descriptor.extract(img_gallery, feature_reference, feature_blob);</div></div><!-- fragment --></li>
<li>Initialize a matcher which using L2 distance. <div class="fragment"><div class="line"><a class="code" href="../../d3/da1/classcv_1_1BFMatcher.html">cv::BFMatcher</a> matcher(<a class="code" href="../../d2/de8/group__core__array.html#ggad12cefbcb5291cf958a85b4b67b6149fa7bacbe84d400336a8f26297d8e80e3a2">NORM_L2</a>);</div><div class="line">std::vector&lt;std::vector&lt;cv::DMatch&gt; &gt; matches;</div></div><!-- fragment --></li>
<li>Have a KNN match on the target and reference images. <div class="fragment"><div class="line">matcher.knnMatch(feature_test, feature_reference, matches, num_candidate);</div></div><!-- fragment --></li>
<li>Print features of the reference images. <div class="fragment"><div class="line">std::cout &lt;&lt; std::endl &lt;&lt; <span class="stringliteral">&quot;---------- Features of target image: &quot;</span> &lt;&lt; target_img &lt;&lt; <span class="stringliteral">&quot;----------&quot;</span> &lt;&lt; endl &lt;&lt; feature_test &lt;&lt; std::endl;</div></div><!-- fragment --> <h2>Results </h2>
</li>
</ul>
</div></div><!-- contents -->
<!-- HTML footer for doxygen 1.8.6-->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Fri Apr 2 2021 11:36:38 for OpenCV by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="../../doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
<script type="text/javascript">
//<![CDATA[
addTutorialsButtons();
//]]>
</script>
</body>
</html>
